<template>
    <div id="app">
        <div class="item" v-for="item in list" :key="item.id" @click="openLink(item)">
            <span>{{ item.publDate }}</span>
            <span>{{ item.title }}</span>
        </div>
        <div class="item tc" v-show="nothing">暂无更多数据</div>
    </div>
</template>

<script>
import {getParamsFromURL, openNativeWindow} from "../script/utils";
import {CLIENT} from "@/common/const";
import {openWeb} from "@/common/common";

import {LIST_KEYS, TYPE_DETAIL, DETAIL_IDS, MARKET_CODES} from "../script/const";
import localforage from "localforage";

const db = localforage.createInstance({
    name: "yngp-1",
});

// 缓存时间: 10分钟
const CACHE_DURATION = 60 * 60 * 1000; // 60*60 * 1000;

const urlParams = getParamsFromURL();
const set = new Set(MARKET_CODES);
const isDapan = set.has(`${urlParams.market}-${urlParams.code}`);
urlParams.urlKey = isDapan ? 3 : urlParams.type; //大盘

export default {
    data() {
        return {
            list: [],
            nothing: false,
        };
    },
   watch:{
    '$route.query':{//监听切换刷新页面
      handler(n,oldval){
        let type= n.type
        let text = {0: "新闻", 1: "公告", 2: "研选",3:"新闻"};
        document.title = text[type];
        if (isDapan && !["0", "3"].includes(type)) {
            this.nothing = true;
            return;
        }
        this.getNewsList();
        this.$router.go(0)//页面刷新
      },
      // 深度观察监听
      deep: true
    }
   },
    mounted() {
        const {type} = urlParams;
        let text = {0: "新闻", 1: "公告", 2: "研选",3:"新闻"};
        document.title = text[type];
        if (isDapan && !["0", "3"].includes(type)) {
            this.nothing = true;
            return;
        }
        this.getNewsList();
    },
    methods: {
        openLink(item){
            let {type} = urlParams;
            console.log(type,'=====type====')
            //新闻使用guid(type:0,3)   公告,研选 使用seq(type:1,2)
            if(['0','3'].includes(type)){
                var query = `${TYPE_DETAIL[type]}=${item.guid}&stockId=${item.stockId}&kcType=${item.kcType || ''}`;
            }else{
                var query = `${TYPE_DETAIL[type]}=${item.seq}&stockId=${item.stockId}&kcType=${item.kcType || ''}`;
            }
            let url = this.urlAbsolute(`/articleDetail/index.html#/?${query}`);
            let text = {0: "新闻", 1: "公告", 2: "研选",3:"新闻"};
            let title = text[type];
            console.log(url,'====url===')
            window.prompt(
                "",
                `up://["open.zx-info-list",${Date.now()},{"url":"${url}","title":"${title}","popuptype":1}]`
            );
        },
        async getNewsList() {
            const {code, market, urlKey, type} = urlParams;
            const URL = LIST_KEYS[urlKey];

            // get data from cache
            /*const CACHE_KEY = `${type}-${market}-${code}`;
            const cache = await db.getItem(CACHE_KEY);
            if (cache && cache.saveTime + CACHE_DURATION > Date.now()) {
                this.list = cache.data;
                if (this.list.length == 0) {
                    this.nothing = true;
                }
                return;
            }*/

            // get data from service
            const params = `stockCode=${type == 3 ? "" : code}&pageNum=0&pageSize=20`;
            const newsURL = `${URL}?` + params;

            const res = await fetch(newsURL);
            const back = await res.json();
            if (back.status != 1) {
                return;
            }
            if (back.data.length < 1) {
                this.nothing = true;
                return;
            }
            this.list = back.data;
            return;

            // data handler
            /*const data = back.data.map((item) => {

                //新闻使用guid(type:0,3)   公告,研选 使用seq(type:1,2)
                if(['0','3'].includes(type)){

                    var query = `${TYPE_DETAIL[type]}=${item.guid}&stockId=${item.stockId}&kcType=${item.kcType || ''}`;

                }else{
                    var query = `${TYPE_DETAIL[type]}=${item.seq}&stockId=${item.stockId}&kcType=${item.kcType || ''}`;
                }
                item.link = this.urlAbsolute(`/articleDetail/index.html#/?${query}`);
                return item;
            });

            this.list = data;

            // save to cache
            db.setItem(CACHE_KEY, {saveTime: Date.now(), data});*/
        },
    },
};
</script>
